<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('表格行内编辑')" />
    <th:block th:include="include :: bootstrap-editable-css" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>名称：</label>
                            <input type="text" id="name" placeholder="yyyy-MM-dd" autocomplete="off">
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="searchData()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <div th:text="${tarSource}" id="tarSource" style="display: none"></div>
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>

<th:block th:include="include :: footer" />
<th:block th:include="include :: bootstrap-table-editable-js" />
<th:block th:include="include :: select2-js" />
<th:block th:include="include :: jquery-cxselect-js" />
<script th:inline="javascript">
    var prefix = ctx + "base/dim/comp/result";
    var pid = "[[${idDimComp}]]";
    var cdDimComp = [[${cdDimComp}]];
    var tarSource = eval('(' + $("#tarSource").html() + ')');

    function searchData() {
        var name = $('#name').val();
        $.ajax({
            url: prefix + "/queryByPid/"+pid,
            type: 'post',
            success: function (result) {
                debugger;
                var data = result.rows;
                var tbodyData=[];
                if(name!="") {
                    $.each(data,function (index,item) {
                        if(item.naSource.indexOf(name)!=-1 || (item.naTar!=null && item.naTar.indexOf(name)!=-1) ) {
                            tbodyData.push(item);
                        }
                    });
                } else {
                    tbodyData = data;
                }
                if (result.code == 0) {
                    var options = {
                        data:tbodyData,
                        showSearch: false,
                        showRefresh: false,
                        showToggle: false,
                        showColumns: false,
                        showPageGo: false,
                        pagination: false,
                        onEditableSave: onEditableSave,
                        columns: [ {
                            field : 'idDimCompResult',
                            title : '主键',
                            visible: false
                        }, {
                            field : 'idDimComp',
                            title : '外键',
                            visible: false
                        },{
                            field : 'cdSource',
                            title : '源编码',
                            width: "20%"
                        },
                            {
                                field : 'naSource',
                                title : '源名称',
                                width: "20%"
                            },
                            {
                                field: 'cdTar',
                                title: '对照目标',
                                width: "45%",
                                align: 'center',
                                editable : {
                                    type : 'select',
                                    title : '状态',
                                    source : eval('(' + $("#tarSource").html() + ')')
                                }
                            }]
                    };
                    $.table.destroy();
                    $.table.init(options);
                }
            }
        });
    }

    $(function() {
        searchData();
    });

    function onEditableSave (field, row, rowIndex, oldValue, $el) {

        //alert("字段名：" + field + "，当前值：" + row[field]  + "，旧值：" + oldValue);
        var tarData = eval('(' + $("#tarSource").html() + ')');
        var naTar="";
        $.each(tarData,function (index,item) {
           if(item.value==row.cdTar) {
               naTar = item.name;
           }
        });
        $.ajax({
            type: "POST",
            url: prefix+"/save",
            data: JSON.stringify({"cdSource":row.cdSource,"naSource":row.naSource,"dimTbSource":row.dimTbSource,"cdTar":row.cdTar,"naTar":naTar,"idPubfldTar":row.idPubfldTar}),
            dataType: 'json',
            contentType: 'application/json;charset=UTF-8',
            success: function(res) {
            }
        });
    }
</script>
</body>
</html>